package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description:
 * @author: LiuXingYu
 * @date: 2021/7/26 10:40
 */
@RestController
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

//    @RequestMapping("/findAll") // 所有的请求类型都支持
    @GetMapping("/findAll")     // 只允许get请求
    public List<User> findAll(){
        return userService.findAll();
    }

    /**
     * Request URL: http://localhost:8090/getUserById?id=1
     */
    @GetMapping("/getUserById")
    /*public User getUserById(HttpServletRequest request,
                            HttpServletResponse response){
        // 底层通过Servlet的request对象进行参数获取
        // 参数获取
        String id = request.getParameter("id");
        System.out.println(id);
        // Servlet获取的参数都是String类型
        // 数据类型转换
        int ids = Integer.parseInt(id);
        return new User();
    }*/
    public User getUserById(Integer id){
        return new User().setId(id);
    }

    /**
     * Request URL: http://localhost:8090/getUser
     * 1.SpringMVC如果解析到参数是对象，先获取其中的get方法
     * 2.把方法get去除首字母再小写，得到对象属性
     * 3.实例化一个user对象，调用set方法赋值
     */
    @GetMapping("getUser")
    public User getUser(User user){
        return user;
    }


}
